// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    fileUrl: app.globalData.fileUrl,
    showIndex:-1,
    TabCur:0,
    title:'',
    videoPlanIngId:'',
    dictList:[],
    videoList: [],
  },
  // 事件处理函数
  onLoad(query) {
      if(query.q){
        const q = decodeURIComponent(query.q)
        const id = q.substring(q.indexOf('id=')+3);
        if(id.length){
            wx.navigateTo({
                url: '/pages/videoshow/index?id='+id,
            })
        }
      }
      this.loadDictData()
  },
  getQueryParams (queryString) {
    // 微信扫码得到的内容进行了一次编码，所以官方要求需要进行decodeURIComponent一次
    queryString = decodeURIComponent(queryString)
    let params = {}
    if (queryString) {
      let queryArray = queryString.split('?')
      if (queryArray.length > 1) {
        let query = queryArray[1]
        let array = query.split('&')
        array.map((value) => {
          let valueArray = value.split('=')
          if (valueArray.length > 1) {
            // 还需要对value进行解码（可能涉及到在value为中文字符，在赋值到二维码前进行了encodeURI编码）
            Object.assign(params, { [valueArray[0]]: decodeURI(valueArray[1]) })
          }
        })
      }
    }
    return params
    },
  loadDictData(){
    app.api.getDictData().then(res=>{
        this.setData({
            dictList:res.data
        },()=>{
            this.seacher()
        })
    })
  },
  tabSelect(e){
      this.setData({
          TabCur:e.currentTarget.dataset.id
      },()=>{
        this.seacher()
      })
  },
  seacher(){
      const tag = this.data.dictList[this.data.TabCur]
    const queryParams = {
        tags:tag,
        title:this.data.title
    }
    let _this = this ;
    app.api.getVideos(queryParams).then(res => {
        if(res.code == 200){
        res.rows.forEach(item=>{
            item.tags = item.tags.split(",");
        })
         _this.setData({
            videoList:res.rows
          })
        }
      })
  },
  bindplayVideo(e){
      console.log(e.target.id); 
      if(this.data.videoPlanIngId == e.target.id){
          return;
      }
      this.setData({
        videoPlanIngId:e.target.id
      })
      const video = wx.createVideoContext(e.target.id)
      video.play();
      for(let i = 0 ; i < this.data.videoList.length; i ++ ){
          const tmepId = 'videoId'+i;
          if(tmepId != e.target.id){
            const tmpvideo =  wx.createVideoContext(tmepId)
            tmpvideo.stop();
          }
      }
    //   video.requestFullScreen({direction:0})
  },
  setTile(e){
      this.setData({
          title:e.detail.value
      })
  },
  selShowIndex(e){
      if(e.currentTarget.dataset.index == this.data.showIndex){
          this.setData({
              showIndex:-1
          })
      }else{
        this.setData({
            showIndex:e.currentTarget.dataset.index
        })
      }
  }
})
